package com.app.qbake.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wind.base.utils.LogUtil;
import com.app.qbake.R;
import com.app.qbake.base.bean.PhotoPreview;
import com.app.qbake.module.publish.event.PhotoRemoveEvent;

import org.greenrobot.eventbus.EventBus;

import uk.co.senab.photoview2.PhotoView;
import uk.co.senab.photoview2.PhotoViewAttacher;


public class PhotoPreviewFragment extends Fragment implements View.OnClickListener {

    protected static final String ARG_PHOTO_POSITION = "args_photo_position";
    protected static final String ARG_STARTING_PHOTO_POSITION = "args_starting_photo_position";
    public static final String ARGS_KEY_PHOTO = "args_photo";

    PhotoView mPhotoView;
    ImageView delteBtn;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutRes(), container, false);
    }

    public int getLayoutRes(){
        return R.layout.wd_fragment_photo_preview;

    }
    protected int mStartingPosition,mPhotoPosition;
    private boolean mIsTransitioning;
    protected PhotoPreview mPhoto;
    private  Boolean showDeleteBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStartingPosition = getArguments().getInt(ARG_STARTING_PHOTO_POSITION);
        mPhotoPosition = getArguments().getInt(ARG_PHOTO_POSITION);
        mIsTransitioning = savedInstanceState == null && mStartingPosition == mPhotoPosition;
        mPhoto= (PhotoPreview) getArguments().getSerializable(ARGS_KEY_PHOTO);
        showDeleteBtn = getArguments().getBoolean(PhotoPreviewActivity.EXTRA_SHOW_DELETE_BTN,false);

    }
    public ImageView getPreviewView() {
        return mPhotoView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView(view);



        final PhotoViewAttacher attacher = new PhotoViewAttacher(mPhotoView);
        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
               getActivity().finish();
            }

            @Override
            public void onOutsidePhotoTap() {
                LogUtil.e("tap","onOutsidePhotoTap");
            }
        });
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.placeholder_bg)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.placeholder_bg);
        if (TextUtils.isEmpty(mPhoto.getPath())){
            showDeleteBtn=false;
            ViewCompat.setTransitionName(mPhotoView, mPhoto.getResId()+"");

            Glide.with(getActivity())
                    .asBitmap()
                    .load(this.mPhoto.getResId())
                    .apply(options)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> glideAnimation) {

                            mPhotoView.setImageBitmap(resource);
                            attacher.update();
                            if (mStartingPosition == mPhotoPosition) {
                                startPostponedEnterTransition(mPhotoView);
                            }
                        }
                    });
        }else {
            ViewCompat.setTransitionName(mPhotoView, mPhoto.getPath());
            Glide.with(getActivity())

                    .asBitmap()
                    .load(this.mPhoto.getPath())
                    .apply(options)
                    .into(new SimpleTarget<Bitmap>() {

                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            mPhotoView.setImageBitmap(resource);
                            attacher.update();
                            if (mStartingPosition == mPhotoPosition) {
                                startPostponedEnterTransition(mPhotoView);
                            }
                        }
                    });
        }
        if (delteBtn==null){
            return;
        }
        if(showDeleteBtn){
            delteBtn.setVisibility(View.VISIBLE);
            delteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //对于从服务器中删除照片，需要先请求服务器
                    EventBus.getDefault().post(new PhotoRemoveEvent(mPhotoPosition));
                }
            });
        }else {
            delteBtn.setVisibility(View.GONE);
        }

    }

    private void initView(View view) {
        mPhotoView= (PhotoView) view.findViewById(R.id.photo_view);
       /* mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                LogUtil.e("tap","onPhotoTap");
            }

            @Override
            public void onOutsidePhotoTap() {
                LogUtil.e("tap","onOutsidePhotoTap");
            }
        });*/
        delteBtn = (ImageView) view.findViewById(R.id.delete_btn);
      /*  tv_fraction = (TextView) view.findViewById(R.id.tv_fraction);
        toolbar_iv_back = view.findViewById(R.id.toolbar_iv_back);
        toolbar_iv_back.setOnClickListener(this);*/
    }








    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.toolbar_iv_back:
                getActivity().finish();
                break;
        }

    }



    public static Fragment newInstance(int position, int startingPosition, PhotoPreview photo, Boolean showDeleteBtn) {
        Bundle args = new Bundle();
        args.putInt(ARG_PHOTO_POSITION, position);
        args.putInt(ARG_STARTING_PHOTO_POSITION, startingPosition);
        args.putSerializable(ARGS_KEY_PHOTO,photo);
        args.putBoolean(PhotoPreviewActivity.EXTRA_SHOW_DELETE_BTN,showDeleteBtn);
        PhotoPreviewFragment fragment = new PhotoPreviewFragment();
        fragment.setArguments(args);
        return fragment;
    }



    private void startPostponedEnterTransition(final View view) {
        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                view.getViewTreeObserver().removeOnPreDrawListener(this);
                ActivityCompat.startPostponedEnterTransition(getActivity());
                return true;
            }
        });
    }


}